package com.le.basic.StackAndQueue;

import java.net.Inet4Address;
import java.util.LinkedList;
import java.util.Queue;

/**
 * 队列模拟栈
 */
public class Code_03_QueueToStack {
    public Queue<Integer> queue;
    public Queue<Integer> help;

    public Code_03_QueueToStack() {
        queue = new LinkedList<>();
        help = new LinkedList<>();
    }

    public void push(Integer num){
        queue.add(num);
    }

    public Integer pop(){
        if (queue.isEmpty()){
            throw new RuntimeException("null");
        }
        while (queue.size() > 1){
            help.add(queue.poll());
        }
        int res = queue.poll();
        swap();
        return res;
    }

    private void swap() {
        Queue<Integer> temp = queue;
        queue = help;
        help = temp;
    }

    public Integer peek(){
        if (queue.isEmpty()){
            throw new RuntimeException("null");
        }
        while (queue.size() != 1){
            help.add(queue.poll());
        }
        int res = queue.poll();
        help.add(res);
        swap();
        return res;
    }

    public static void main(String[] args) {
        Code_03_QueueToStack toStack = new Code_03_QueueToStack();
        toStack.push(1);
        toStack.push(2);
        toStack.push(3);

        System.out.println(toStack.peek());
        System.out.println(toStack.pop());
        System.out.println(toStack.pop());
    }


}
